package ${pkg};

import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;

import com.blue.jdbc.Page;
import com.blue.core.dict.annotation.DictParser;
import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;

import ${corePkg}.util.Const;
import ${facadePkg}.${clazz}Facade;
import ${exceptionPkg}.${clazz}Exception;
import ${messagePkg}.${clazz}Request;
import ${messagePkg}.${clazz}Response;
<#if impList??>
<#list impList as vo>
import ${vo};
</#list>
</#if>

/**
 * ${name}控制器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 ${.now?string("yyyy年MM月dd日")}
 */
@Controller
@Http(url = "/${module}")
public class ${clazz}Controller
{
	@Autowired
	private ${clazz}Facade ${field}Facade;
	
	private static DictParser parser = DictParser.getInstance();
	
	public ${clazz}Controller()
	{
	}

	@Http(url = "/${view}_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		${clazz}Request ${field}Request = new ${clazz}Request();
		Page<${clazz}Response> page = ${field}Facade.listPage(${field}Request, Const.PAGE_START, Const.PAGE_SIZE);
		return View.createBuilder().setView("/${module}/${view}_list")
						.put("model", ${field}Request)
						.put("map", parser.getStringMap())
						.put("page", page)
						.build();
	}

	@Http(url = "/${view}_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		${clazz}Request ${field}Request = request.getPostObject(${clazz}Request.class);
		int start = request.getPostInt("start", Const.PAGE_START);
		Page<${clazz}Response> page = ${field}Facade.listPage(${field}Request, start, Const.PAGE_SIZE);
		return View.createBuilder().setView("/${module}/${view}_list_grid")
						.put("page", page)
						.build();
    }

    @Http(url = "/${view}_get", method = HttpMethod.GET)
    public View get(HttpRequest request)
    {
        int id = request.getQueryInt("id", 0);
        ${clazz}Response response = null;
        if (id > 0)
        {
            response = ${field}Facade.get(id);
        }
        return View.createBuilder().setView("/${module}/${view}_get")
                        .put("model", response == null ? new ${clazz}Response() : response)
                        .put("map", parser.getStringMap())
                        .build();
    }

    @Http(url = "/${view}_save", method = HttpMethod.POST)
    public void save(HttpRequest request)
    {
        ${clazz}Request ${field}Request = request.getPostObject(${clazz}Request.class);
        ${clazz}Response response = null;
        if (${field}Request.getId() == null)
        {
            ValidationUtil.valid(${field}Request, SaveModel.class);
            response = ${field}Facade.add(${field}Request);
        }
        else
        {
            ValidationUtil.valid(${field}Request, UpdateModel.class);
            response = ${field}Facade.update(${field}Request);
        }
    }

}
